package com.ruoyi.web.utils;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.aliyuncs.sts.model.v20150401.AssumeRoleRequest;
import com.aliyuncs.sts.model.v20150401.AssumeRoleResponse;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.aliyuncs.vod.model.v20170321.DeleteVideoResponse;
import com.google.gson.Gson;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class VideoUpload {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    //设置好账号的ACCESS_KEY和SECRET_KEY
    //账号AK信息（必选）
    private static final String accessKeyId = "LTAI4G9quN9jPg8u81QXCDgM";
    //账号AK信息（必选）
    private static final String accessKeySecret = "Tzq2N3R3eVuuGOgnIVDA57aMkOZSqf";

    private static final String roleArn = "acs:ram::1331724891926589:role/video";

    public static DefaultAcsClient initVodClient() throws ClientException {
        String regionId = "cn-shanghai";  // 点播服务接入区域
        DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret);
        DefaultAcsClient client = new DefaultAcsClient(profile);
        return client;
    }


    /*请求示例*/
//    public static void main(String[] argv) throws ClientException {
//        DefaultAcsClient client = initVodClient();
//        DeleteVideoResponse response = new DeleteVideoResponse();
//        try {
//            response = deleteVideo(client);
//        } catch (Exception e) {
//            System.out.print("ErrorMessage = " + e.getLocalizedMessage());
//        }
//        System.out.print("RequestId = " + response.getRequestId() + "\n");
//    }

    public static AssumeRoleResponse assumeRole(String roleSessionName, String policy) throws ClientException {
        try {
            //构造default profile（参数留空，无需添加Region ID）
            /*
            说明：当设置SysEndpoint为sts.aliyuncs.com时，regionId可填可不填；反之，regionId必填，根据使用的服务区域填写，例如：cn-shanghai
            详情参考STS各地域的Endpoint，请参见接入地址（https://help.aliyun.com/document_detail/66053.html?spm=a2c4g.11186623.2.16.1db87074dzvl3J#reference-sdg-3pv-xdb）。
             */
//            DefaultProfile.addEndpoint("", "cn-shanghai", "Sts", "sts.cn-shanghai.aliyuncs.com");
            IClientProfile profile = DefaultProfile.getProfile("cn-shanghai", accessKeyId, accessKeySecret);
            IAcsClient client = new DefaultAcsClient(profile);
            AssumeRoleRequest request = new AssumeRoleRequest();
            //用profile构造client
//            DefaultAcsClient client = new DefaultAcsClient(profile);
            // 创建一个 AssumeRoleRequest 并设置请求参数
//            final AssumeRoleRequest request = new AssumeRoleRequest();
//            request.setSysEndpoint("sts.aliyuncs.com");
//            request.setEndpointType("sts.cn-shanghai.aliyuncs.com");
            request.setRegionId("cn-shanghai");
            request.setRoleArn(roleArn);
            request.setRoleSessionName(roleSessionName);

            AssumeRoleResponse response = client.getAcsResponse(request);
            System.out.println(new Gson().toJson(response));
//            request.setPolicy(policy);
            // 发起请求，并得到response
//            final AssumeRoleResponse response = client.getAcsResponse(request);
            return response;
        } catch (ClientException e) {
            throw e;
        }
    }


    /**
     * 删除视频
     * @param client 发送请求客户端
     * @return DeleteVideoResponse 删除视频响应数据
     * @throws Exception
     */
    public static DeleteVideoResponse deleteVideo(DefaultAcsClient client,String imgId) throws Exception {
        DeleteVideoRequest request = new DeleteVideoRequest();
        //支持传入多个视频ID，多个用逗号分隔
        request.setVideoIds(imgId);
        return client.getAcsResponse(request);
    }
}
